$(function () {

    $('#side-menu ul').eq(0).addClass('in');

    //获取当前两天时间
    // function toDou(iNum){
    //     return iNum<10?'0'+iNum:iNum;
    // }
    // function GetDateStr(AddDayCount) {
    //     var oDate = new Date();
    //     oDate.setDate(oDate.getDate()+AddDayCount);//获取AddDayCount天后的日期
    //     var y = oDate.getFullYear();
    //     var m = oDate.getMonth()+1;//获取当前月份的日期
    //     var d = oDate.getDate();
    //     return toDou(y)+"-"+toDou(m)+"-"+toDou(d);
    // }
    // var startTime = GetDateStr(-1);
    // var endTime = GetDateStr(0);
    // $('input[name=startTime]').val(startTime);
    // $('input[name=endTime]').val(endTime);

    var start = {
        elem: '#startTime',
        event:"focus",
        format: 'YYYY-MM-DD', //自动生成的时间格式
        min: '1900-01-01', //创建当前日期为最小日期
        max: '2099-06-16', //创建最大日期
        choose: function (datas) {
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };
    var end = {
        elem: '#endTime',
        event:"focus",
        format: 'YYYY-MM-DD', //自动生成的时间格式
        min: '1900-01-01',
        max: '2099-06-16', //创建最大日期
        choose: function (datas) {
            start.max = datas; //结束日选好后，重置开始日的最大日期
        }
    };
    laydate(start);
    laydate(end);

    getZUsersList();
    $('#btn_search').on('click',function(){
        $("#pageNum").val(1);
        getZUsersList();
    })

    $(document).keydown(function(event){
        if(event.keyCode==13){
            $("#btn_search").click();
        }
    });
});


/**
 *  功能描述：获取专线列表
 *  请求方式：GET
 *  请求地址：/api/line_user/list
 *  函数名称：getZUsersList
 */
function getZUsersList() {
    $.ajax({
        url: whole_path+'/api/line_user/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: $('#user_list_form').serialize(),
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json.result;
                var temp = "";
                var operation,unbind,modifyTime = '';
                if (list != null && list.length>0){
                    $.each(list, function (index, item) {

                        unbind = ' <a class="btn-sm btn-warning" data-toggle="modal" data-target="#unbindModal" title="解绑"  onclick="userUnbind('+item.id+',\''+encodeURIComponent(item.userName)+'\')"><i class="fa fa-magnet"></i></a>&nbsp; ';
                        modifyTime = ' <a class="btn-sm btn-danger" data-toggle="modal" data-target="#reviseDateModal" title="修改到期时间" onclick="reviseTime('+item.id+',\''+encodeURIComponent(item.userName)+'\')"><i class="fa fa-calendar"></i></a>&nbsp; <a class="btn-sm navy-bg" href="../cuser/cars_team_list.jsp?id='+item.id+'" title="我的车队" ><i class="fa fa-truck"></i></a>';
                        operation = ' <a class="btn-sm btn-info" href="zuser_edit.jsp?id='+item.id+'"  title="编辑"><i class="fa fa-pencil-square-o"></i></a>&nbsp; <a href="zuser_detail.jsp?id='+item.id+'&account='+item.userName+'" class="btn-sm blue-bg" title="详情"><i class="fa fa-list-alt"></i></a> &nbsp;'+unbind+modifyTime;

                        temp += '<tr>'
                            + '<td data-title="帐号">' + item.userName + '</td>'
                            + '<td data-title="注册时间">' + timeFormat(item.registerTime) + '</td>'
                            + '<td data-title="用户姓名">' + item.realName + '</td>'
                            + '<td data-title="联系电话">' + item.phoneNumber + '</td>'
                            + '<td data-title="拥有车辆数">' + item.carsCount + '</td>'
                            + '<td data-title="开户人">' + (item.operateName == null?'':item.operateName) + '</td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#leased_line_list tbody').html(temp);
                    $("[data-toggle='tooltip']").tooltip();
                    page('#pagination', json.pagecount, json.pageindex, json.pagesize, getZUsersList, '#pageNum');

                }else{
                    $.toast("没有查到数据",3000);
                    $('#leased_line_list tbody').html('');
                    if($('#pagination').html().length > 0){
                        $('#pagination').jqPaginator('destroy');
                    }
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}


/**
 *  功能描述：获取到期时间列表
 *  请求方式：GET
 *  请求地址：/api/limit/list
 *  函数名称：reviseTime
 */
function reviseTime(id,name) {
    $.ajax({
        url: whole_path+'/api/limit/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: {
            userId:id,
            userType:2
        },
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json;
                var temp = "";
                if (list != null && list.length>0){
                    var operation = ''; //操作按钮
                    var enabled = '';
                    $.each(list, function (index, item) {
                        operation = '<a class="btn-sm btn-danger btn-rounded" onclick="submitDate('+item.id+','+index+',\''+encodeURIComponent(name)+'\');">提交</a>';

                        if(item.beUsed == 1){
                            enabled = '<input type="radio" name="beUsed'+index+'" value="1" checked>是<input type="radio" name="beUsed'+index+'" value="0">否';
                        }else{
                            enabled = '<input type="radio" name="beUsed'+index+'" value="1">是<input type="radio" name="beUsed'+index+'" value="0" checked>否';
                        }

                        var loginType = item.platformType;
                        switch (loginType){
                            case 0:
                                loginType = "PC"
                                break;
                            case 1:
                                loginType = "iOS"
                                break;
                            case 2:
                                loginType = "Android"
                                break;
                        }

                        temp += '<tr class="gradeA">'
                            + '<td data-title="登录类型">' + loginType + '</td>'
                            + '<td data-title="是否开通">'+enabled+'</td>'
                            + '<td data-title="到期时间"><input id="expireTime'+index+'" name="expireTime" class="laydate-icon form-control layer-date" onclick="laydate()" value="'+timeFormat(item.expireTime)+'"></td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#revise_date_list tbody').html(temp);
                }else{
                    $.toast("没有查到数据",3000);
                    $('#revise_date_list tbody').html('');
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}

/**
 *  功能描述：提交修改时间
 *  请求方式：POST
 *  请求地址：/api/limit/update
 *  函数名称：submit_date
 */
function submitDate(id,index,name){
    $('#reviseDateModal').modal('hide');
    var beUsed = $("input[name='beUsed"+index+"']:checked").val();
    var expireTime = $("#expireTime"+index).val();
    $.ajax({
        url:whole_path+ '/api/limit/update',
        type: 'POST',
        dataType: 'json',
        data: {
            'id':id,
            'beUsed':beUsed,
            'expireTime':expireTime,
            'isUnbind':0,
            'userName':name,
            'userType':2
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                getZUsersList();
            }else{
                $.toast(data.msg,3000);
            }

        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })

}



/**
 *  功能描述：获取解绑数据列表
 *  请求方式：GET
 *  请求地址：/api/limit/list
 *  函数名称：userUnbind
 */
function userUnbind(id,name) {
    $.ajax({
        url: whole_path+'/api/limit/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: {
            userId:id,
            userType:2
        },
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json;
                var temp = "";
                if (list != null && list.length>0){
                    var operation = ''; //操作按钮
                    $.each(list, function (index, item) {
                        var loginType = item.platformType;
                        switch (loginType){
                            case 0:
                                loginType = "PC"
                                break;
                            case 1:
                                loginType = "iOS"
                                break;
                            case 2:
                                loginType = "Android"
                                break;
                        }
                        if (item.hardware != null && item.hardware != ''){
                            operation = '<a class="btn-sm btn-warning btn-rounded" onclick="modifyUnbind('+item.id+',\''+encodeURIComponent(name)+'\');">解绑</a>';
                        }else {
                            operation = '<a class="btn-sm btn-info btn-rounded" >已解绑</a>';
                        }
                        temp += '<tr class="gradeA">'
                            + '<td data-title="登录类型">' + loginType + '</td>'
                            + '<td data-title="操作">' + operation + '</td>'
                            + '</tr>';
                    })
                    $('#user_limit_unbind_list tbody').html(temp);
                }else{
                    $.toast("暂无解绑的数据",3000);
                    $('#user_limit_unbind_list tbody').html('');
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}



/**
 *  功能描述：解绑
 *  请求方式：POST
 *  请求地址：/api/limit/update
 *  函数名称：modifyUnbind
 */

function modifyUnbind(id,name){
    $('#unbindModal').modal('hide');
    $.ajax({
        url:whole_path+ '/api/limit/update',
        type: 'POST',
        dataType: 'json',
        data: {
            'id':id,
            'isUnbind':1,
            'userName':name,
            'userType':2
        },
        beforeSend:function(){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:false,canCancel:true});
        },
        success:function(data){
            if(data.status == 0){
                $.toast('操作成功',3000);
                getZUsersList();
            }else{
                $.toast(data.msg,3000);
            }

        },
        complete:function(){
            $.progressBar().close();
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })

}